<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script type="text/javascript">

    /*
        post API
        1. FormData 类型
     */
    const url = "http://47.106.187.222/message/message"
    let obj = {userId: 22, message: "我是用来测试api的"}

    function submitData() {
        let xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4) {
                if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) {
                    console.log(xhr.responseText);
                } else {
                    console.log("Request was unsuccessful: " + xhr.status);
                }
            }
        };
        xhr.open("post", url, true);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        // 如果要发送数据的话,需要序列化
        // 使用FormData可以不用设置请求头了
        // 还可以通过 .append(key,value)方法设置
        let data = new FormData()
        data.append('userId',"22")
        data.append('message',"你好啊")
        xhr.send(data);
        // xhr.send(null);
    }
    submitData();

  
    /*
        2. 超时
        xhr.timeout = 1000; // 设置 1 秒超时 xhr.ontimeout = function() {
        alert("Request did not return in a second."); };
     */
    
    /*
        3. overrideMimeType()方法

     */
    


</script>
</body>
</html>